package net.oschina.app.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import net.oschina.app.R;
import net.oschina.app.bean.Comment;
import net.oschina.app.bean.CommentList;
import net.oschina.app.ui.view.CircleImageView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Ethan on 2017/4/7.
 */

public class NewsItemCommentAdapter extends BaseRecyclerViewAdapter {

    private Context mContext;
    private List<Comment> list = new ArrayList<>();

    //构造函数
    public NewsItemCommentAdapter(Context context, CommentList commentList) {
        super(context);
        this.mContext = context;
        this.list = commentList.getList();
    }

    //找到展示布局,并绑定Holder
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = mInflater.inflate(R.layout.view_xrv_newsitem_comment, parent, false);
        NewsItemCommentHolder holder = new NewsItemCommentHolder(itemView);
        return holder;
    }

    //获取数据,并将数据绑定到控件
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        super.onBindViewHolder(holder, position);
        Comment comment = list.get(position);
        NewsItemCommentHolder commentHolder = (NewsItemCommentHolder) holder;
        commentHolder.mTvAuthor.setText(comment.getAuthor());
        List<Comment.Refer> refers = comment.getRefers();
        if (refers.size()==0){
            commentHolder.mLlCommentReply.setVisibility(View.GONE);
        }else{
            commentHolder.mTvRefertitle.setText(refers.get(0).getRefertitle());
            commentHolder.mTvReferbody.setText(refers.get(0).getReferbody());
        }
        commentHolder.mTvPubdate.setText(comment.getPubDate());
        commentHolder.mTvContent.setText(comment.getContent());
        switch (comment.getAppClient()){
            case 0:
                commentHolder.mTvAppclient.setText("iPhone 6s");
                break;
            case 1:
                commentHolder.mTvAppclient.setText("锤子手机");
                break;
            case 2:
                commentHolder.mTvAppclient.setText("魅族手机");
                break;
            case 3:
                commentHolder.mTvAppclient.setText("小米 Note");
                break;
            case 4:
                commentHolder.mTvAppclient.setText("华为手机");
                break;
            default:
                commentHolder.mTvAppclient.setText("Android");
        }
    }

    //条目数量
    @Override
    public int getItemCount() {
        return list.size();
    }

    //创建ViewHolder类
    public class NewsItemCommentHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.civ_portrait)
        CircleImageView mCivPortrait;
        @BindView(R.id.tv_author)
        TextView mTvAuthor;
        @BindView(R.id.tv_pubdate)
        TextView mTvPubdate;
        @BindView(R.id.tv_refertitle)
        TextView mTvRefertitle;
        @BindView(R.id.tv_referbody)
        TextView mTvReferbody;
        @BindView(R.id.ll_comment_reply)
        LinearLayout mLlCommentReply;
        @BindView(R.id.tv_content)
        TextView mTvContent;
        @BindView(R.id.tv_appclient)
        TextView mTvAppclient;
        @BindView(R.id.ll_newsitem_comment)
        LinearLayout mLlNewsitemComment;

        public NewsItemCommentHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
